<?php

class Group_Model_Entry
{
	protected $_id = null;
	protected $_idname = null;
	protected $_classIdName = NULL;
	protected $_nicename = null;


    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }


	public function setId($id = null)
	{
		$this->_id = $id;
		return $this;
	}
	public function setIdName($idname = null)
	{
		$this->_idname = $idname;
		return $this;
	}
	public function setClassIdName($classIdName)
	{
		$this->_classIdName = $classIdName;
		return $this;
	}
	public function setNicename($nicename = null)
	{
		$this->_nicename = $nicename;
		return $this;
	}

	public function getId()
	{
		return $this->_id;
	}
	public function getIdName()
	{
		return $this->_idname;
	}
	public function getClassIdName()
	{
		return $this->_classIdName;
	}
	public function getNicename()
	{
		return $this->_nicename;
	}


	public function getUrl($params=NULL)
	{
		return '/group/'. $this->getIdName();
	}

	/**
	 * Check the access to an object associated to this object
	 */
	public function userHasObjectAccess($accountId, $objClassIdName, $objId, $accessName)
	{
		//TODO: load these from db
		$member = Group_Model_Member::groupGetMember($this->getId(), $accountId, TRUE);
		switch ($objClassIdName) {
		case 'note':
			switch ($accessName) {
			case 'GET': return TRUE;
			case 'PUT': return $member === NULL ? FALSE : $member->hasRole('admin');
			case 'UPDATE': return $member === NULL ? FALSE : $member->hasRole('admin');
			case 'GET-comment': return TRUE;
			case 'PUT-comment': return TRUE;
		//	case 'UPDATE-comment': return TRUE;
			default: return FALSE;
			}
		case 'forum':
			switch ($accessName) {
			case 'GET': return TRUE;
			case 'PUT': return $member != NULL;
			case 'UPDATE': return $member === NULL ? FALSE : $member->hasRole('admin');
			case 'DELETE': return $member === NULL ? FALSE : $member->hasRole('admin');
			case 'GET-comment': return TRUE;
			case 'PUT-comment': return $member != NULL;
		//	case 'UPDATE-comment': return TRUE;
			default: return FALSE;
			}
		case 'wall':
			switch ($accessName) {
			case 'GET': return TRUE;
			case 'PUT': return $member != NULL;
			case 'DELETE': return $member === NULL ? FALSE : $member->hasRole('admin');
			case 'GET-comment': return TRUE;
			case 'PUT-comment': return $member != NULL;
			default: return FALSE;
			}
		}
		return FALSE;
	}



    public function __set($name, $value)
    {
        $method = 'set' . ucfirst($name);
        $this->$method($value);
    }
    public function __get($name)
    {
        $method = 'get' . ucfirst($name);
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }



	protected static $_instanceList = array();


	/**
	 * Queries for a loaded instance.
	 */
	public static function findInstanceById($groupId = null)
	{
		if (intval($groupId) > 0 && isset(self::$_instanceList[$groupId]) === TRUE) {
			return self::$_instanceList[$groupId];
		}
		return NULL;
	}
	public static function findInstanceByIdName($idname = null)
	{
		if (empty($idname) === FALSE) {
			$idname = strtolower($idname);
			foreach (self::$_instanceList as $inst){
				if ($inst->getIdName() === $idname) {
					return $inst;
				}
			}
		}
		return NULL;
	}

	/**
	 * Queries for an instance. If the instance is not found, it will be loaded from the database.
	 */
	public static function getInstanceById($accountId = null)
	{
		return self::getInstance('id', $accountId);
	}
	public static function getInstanceByIdName($idname = null)
	{
		return self::getInstance('idname', $idname);
	}
	// The key is either 'id' or 'idname'
	public static function getInstance($key = NULL, $value = NULL)
	{
		if ($key === 'id') {
			$inst = self::findInstanceById($value);
		} else if ($key === 'idname') {
			$inst = self::findInstanceByIdName($value);
			$value = strtolower($value);
		} else {
			return NULL;
		}

		if ($inst instanceof Group_Model_Entry) {
			return $inst;
		}

		$inst = new Group_Model_Entry();

		$table = Group_Model_DbTable_Entry::getInstance();

		$select = $table->select();
		$select->where($key .'= ?', $value);

		$dbResult = $table->fetchAll($select);

		if ($dbResult->count() !== 1) {
			return NULL;
		}

		$dbRow = $dbResult->getRow(0);
		$inst
			->setId($dbRow->id)
			->setIdName($dbRow->idname)
			->setClassIdName($dbRow->class)
			->setNicename($dbRow->nicename)
		;

		self::$_instanceList[$dbRow->id] = $inst;

		return $inst;
	}

	public static function getNewests($count = 10)
	{
		$instances = array();


		$table = Group_Model_DbTable_Entry::getInstance();

		//TODO: filters
		$select = $table->select();
		$select
			->order('time DESC')
			->limit($count)
		;

		$dbResult = $table->fetchAll($select);

		foreach ($dbResult as $dbRow) {
			if (isset(self::$_instanceList[$dbRow->id])) {
				$instances[] = self::$_instanceList[$dbRow->id];
			} else {
				$inst = new Group_Model_Entry();
				$inst
					->setId($dbRow->id)
					->setIdName($dbRow->idname)
					->setClassIdName($dbRow->class)
					->setNicename($dbRow->nicename)
				;

				self::$_instanceList[$dbRow->id] = $inst;
				$instances[] = $inst;
			}
		}

		return $instances;
	}
}